﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace XOS_VHD_Tools.VHD
{
    /*
0x00	Empty partition entry
0x01	FAT12 as primary partition in first physical 32 MB of disk or as logical drive anywhere on disk (else use 0x06 instead)
0x02	XENIX root
0x03	XENIX usr
0x04	FAT16 with less than 65536 sectors (32 MB). As primary partition it must reside in first physical 32 MB of disk, or as logical drive anywhere on disk (else use 0x06 instead).
0x05	Extended partition with CHSaddressing
0x06	FAT16B with 65536 or more sectors. It must reside in first physical 8 GB of disk, unless used for logical drives in an0x0F extended partition (else use 0x0E instead). Also used for FAT12 and FAT16 volumes in primary partitions if they are not residing in first physical 32 MB of disk.[nb 1]
0x07	IFS/HPFS/NTFS/exFAT
0x08	Logical sectored FAT12 or FAT16[3][nb 3]
	    OS/2
	    AIX
	    QNX "qny"[2][nb 2]
	    
	    partition spanning multiple drives
0x09	AIX bootable
	    QNX "qnz"[2][nb 2]
	    Coherent file system
0x0A	OS/2 Boot Manager[nb 4]
	    Coherent swap partition
	    Open Parallel Unisys Server
0x0B	FAT32 with CHS addressing
0x0C	FAT32X with LBA
0x0E	FAT16X with LBA
0x0F	Extended partition with LBA
0x11	Logical sectored FAT12 or FAT16[3][nb 3]
	    Hidden FAT12 (corresponds with 0x01)[nb 4]
0x12	configuration partition (bootableFAT)[4]
	    hibernation partition[4]
	    diagnostics and firmware partition (bootable FAT)[4]
	    service partition (bootableFAT)[4] (see 0x98)
	    Rescue and Recovery partition[4]
0x14	Logical sectored FAT12 or FAT16[3][nb 3]
	    Hidden FAT16 (corresponds with 0x04)[nb 4]
0x15	Hidden extended partition withCHS addressing (corresponds with 0x05)[nb 4]
0x16	Hidden FAT16B (corresponds with 0x06)[nb 4]
0x17	Hidden IFS (corresponds with0x07)[nb 4]
	    Hidden HPFS (corresponds with 0x07)[nb 4]
	    Hidden NTFS (corresponds with 0x07)[nb 4]
	    Hidden exFAT (corresponds with 0x07)[nb 4]
0x18	AST Zero Volt Suspend or SmartSleep partition
0x19	Willowtech Photon coS (see0x20)
0x1B	Hidden FAT32 (corresponds with 0x0B)[nb 4]
0x1C	Hidden FAT32X with LBA (corresponds with 0x0C)[nb 4]
0x1E	Hidden FAT16X with LBA (corresponds with 0x0E)[nb 4]
0x1F	Hidden extended partition withLBA addressing (corresponds with 0x0F)[nb 4]
0x20	Windows Mobile update XIP
	    Willowsoft Overture File System (OFS1) (see 0x19)
0x21	HP Volume Expansion (SpeedStor)[nb 5]
	    FSo2 (Oxygen File System) (see 0x22)
0x22	Oxygen Extended Partition Table (see 0x21)
0x23	Reserved
	    Windows Mobile boot XIP
0x24	Logical sectored FAT12 or FAT16[3][nb 3]
0x25	Windows Mobile IMGFS
0x26	Reserved
0x27	Windows recovery environment (RE) partition (hidden NTFSpartition type 0x07)[5]
	    FAT32 or NTFS rescue partition
	    MirOS partition
	    RooterBOOT kernel partition (contains a raw ELF Linux kernel, no filesystem)
0x2A	AtheOS file system (AthFS, AFS)
0x2B	SyllableSecure (SylStor), a variant of AthFS
0x31	Reserved
0x32	
0x33	Reserved
0x34	Reserved
0x35	JFS (OS/2 implementation of AIX Journaling Filesystem), non-bootable
0x36	Reserved
0x38	THEOS version 3.2, 2 GB partition
0x39	Plan 9 edition 3 partition (sub-partitions described in second sector of partition)
	    THEOS version 4 spanned partition
0x3A	THEOS version 4, 4 GB partition
0x3B	THEOS version 4 extended partition
0x3C	PqRP (PartitionMagic in progress)[6]
0x3D	Hidden NetWare
0x40	PICK R83
	    Venix 80286
0x41	Personal RISC Boot
	    Old Linux/Minux (disk shared with DR DOS 6.0) (corresponds with 0x81)
	    PPC PReP (Power PC Reference Platform) Boot
0x42	Secure Filesystem (SFS)
	    Old Linux swap (disk shared with DR DOS 6.0) (corresponds with 0x82)
	    Dynamic extended partition marker
0x43	Old Linux native (disk shared with DR DOS 6.0) (corresponds with 0x83)
0x44	Norton GoBack, WildFile GoBack, Adaptec GoBack, Roxio GoBack
0x45	Priam (see also 0x5C)
	    Boot-US boot manager
	    EUMEL/ELAN
0x46	EUMEL/ELAN
0x47	EUMEL/ELAN
0x48	EUMEL/ELAN
0x4A	ALFS/THIN advanced lightweight filesystem for DOS
0x4C	Aos (A2) filesystem (76)
0x4D	Primary QNX POSIX volume on disk[2][nb 2]
0x4E	Secondary QNX POSIX volume on disk[2][nb 2]
0x4F	Tertiary QNX POSIX volume on disk[2][nb 2]
	    Nat filesystem (79)
0x50	Alternative Nat filesystem (80)
	    Read-only partition (old)
	    Lynx RTOS
0x51	
	    Read-write partition (Aux 1)
0x52	CP/M
	
0x53	Aux 3
0x54	Dynamic Drive Overlay (DDO)
0x55	EZ-Drive, Maxtor, MaxBlast, or DriveGuide INT 13h redirector volume
0x56	Logical sectored FAT12 or FAT16[3][nb 3]
	    DiskManager partition converted to EZ-BIOS
	    VFeature partitionned volume
0x57	
	    VNDI partition
0x5C	Priam EDisk Partitioned Volume (see also 0x45)
0x64	NetWare File System 286
0x65	NetWare File System 386
0x78	XOSL bootloader filesystem[citation needed]
0x80	Old Minix file system
0x81	MINIX file system (corresponds with 0x41)
0x82	Linux swap space(corresponds with 0x42)
	    Solaris
0x83	Any native Linux file system(corresponds with 0x43)
0x84	Hibernation (suspend to disk, S2D)[4][7]
0x85	Linux extended[8]
0x86	Legacy FT FAT16
0x87	Legacy FT NTFS
0x88	Linux plaintext
0x8B	Legacy FT FAT32
0x8C	Legacy FT FAT32 with LBA
0x8D	Hidden FAT12 (corresponds with 0x01)[nb 6]
0x8E	Linux LVM
0x90	Hidden FAT16 (corresponds with 0x04)[nb 6]
0x91	Hidden extended partition withCHS addressing (corresponds with 0x05)[nb 6]
0x92	Hidden FAT16B (corresponds with 0x06)[nb 6]
0x97	Hidden FAT32 (corresponds with 0x0B)[nb 6]
0x98	Hidden FAT32X (corresponds with 0x0C)[nb 6]
	    service partition (bootableFAT)[4] (see 0x12)
0x9A	Hidden FAT16X (corresponds with 0x0E)[nb 6]
0x9B	Hidden extended partition withLBA (corresponds with0x0F)[nb 6]
0xA0	Diagnostic partition for HP laptops[4]
0xA1	HP Volume Expansion (SpeedStor)[nb 5]
0xA3	HP Volume Expansion (SpeedStor)[nb 5]
0xA4	HP Volume Expansion (SpeedStor)[nb 5]
0xA5	BSD slice[9]
0xA6	HP Volume Expansion (SpeedStor)[nb 5]
	    OpenBSD slice
0xA7	NeXTSTEP
0xA8	Apple Mac OS X[nb 7]
0xA9	NetBSD slice[10]
0xAB	Apple Mac OS X boot[nb 7]
0xAF	Apple Mac OS X HFS andHFS+[nb 7]
0xB1	HP Volume Expansion (SpeedStor)[nb 5]
	    QNX Neutrino power-safe file system[nb 2]
0xB2	QNX Neutrino power-safe file system[nb 2]
0xB3	HP Volume Expansion (SpeedStor)[nb 5]
	    QNX Neutrino power-safe file system[nb 2]
0xB4	HP Volume Expansion (SpeedStor)[nb 5]
0xB6	HP Volume Expansion (SpeedStor)[nb 5]
0xC0	Secured FAT partition (smaller than 32 MB)[nb 8][nb 9]
0xC1	Secured FAT12 (corresponds with 0x01)[nb 8]
0xC4	Secured FAT16 (corresponds with 0x04)[nb 8]
0xC5	Secured extended partition withCHS addressing (corresponds with 0x05)[nb 8]
0xC6	Secured FAT16B (corresponds with 0x06)[nb 8]
0xCB	Secured FAT32 (corresponds with 0x0B)[nb 8]
0xCC	Secured FAT32X (corresponds with 0x0C)[nb 8]
0xCE	Secured FAT16X (corresponds with 0x0E)[nb 8]
0xCF	Secured extended partition withLBA (corresponds with0x0F)[nb 8]
0xD0	Secured FAT partition (larger than 32 MB)[nb 10][nb 9]
0xD1	Secured FAT12 (corresponds with 0x01)[nb 10]
0xD4	Secured FAT16 (corresponds with 0x04)[nb 10]
0xD5	Secured extended partition withCHS addressing (corresponds with 0x05)[nb 10]
0xD6	Secured FAT16B (corresponds with 0x06)[nb 10]
0xDB	CP/M-86, Concurrent CP/M-86, Concurrent DOS[11]
0xDE	Dell diagnostic partition[4]
0xE5	Logical sectored FAT12 or FAT16[3][nb 3]
0xEB	BFS
0xED	EDC
0xEE	EFI protective MBR[12]
0xEF	EFI system partition can be aFAT file system
0xF2	Logical sectored FAT12 or FAT16[3][nb 3]
0xFB	VMware VMFS
0xFC	VMware VMKCORE
0xFD	Linux RAID auto
0xFE	IBM IML partition[4]
0xFF	XENIX bad block table

     */
    public enum PartitionType : byte
    {
        Empty = 0,

        ExtendedPartition = 0x05,
        ExtendedPartition_LBA = 0x0F,

        HFSPlus = 0xaf,

        FAT32 = 0x0B,
        FAT32_LBA = 0x0C,

        FAT32_HIDDEN = 0x1B,
        FAT32_LBA_HIDDEN = 0x1C,

        FAT32_SECURED = 0xCB,
        FAT32_LBA_SECURED = 0xCC,

        NTFS = 0x07,
        NTFS_HIDDEN = 0x17,
        NTFS_EXTENDED = 0xC7,
    }



    public enum ActivityFlag : byte
    {
        Active = 0x80,
        Nonactive = 0x00
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 16)]
    public struct MBR_ptEntry
    {
        // 0x00	1	status (0x80 = bootable (active), 0x00 = non-bootable, other = invalid ) [a]
        public ActivityFlag active;
        //0x01	3	CHS address of first absolute sector in partition. [b] The format is described by 3 bytes, see the next 3 rows.
        //0x01	1	h7-0	head [c]
        //0x02	1	c9‑8	s5–0	sector in bits 5–0; bits 7–6 are high bits of cylinder [c]
        //0x03	1	c7-0	bits 7–0 of cylinder [c]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        byte[] CHS_start;
        // 0x04	1	Partition type[7], http://en.wikipedia.org/wiki/Partition_type
        public PartitionType type;
        //0x05	3	CHS address of last absolute sector in partition. [b] The format is described by 3 bytes, see the next 3 rows.
        //0x05	1	h7-0	head [c]
        //0x06	1	c9‑8	s5–0	sector in bits 5–0; bits 7–6 are high bits of cylinder [c]
        //0x07	1	c7-0	bits 7–0 of cylinder
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        byte[] CHS_end;
        // 0x08	4	LBA of first absolute sector in the partition [d]
        public uint LBA_start;
        // 0x0C	4	Number of sectors in partition [d]
        public uint LBA_numOfSect;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 512)]
    public struct MBR
    {
        //0	 446	 MBR Bootstrap (flat binary executable code)
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 446)]
        private byte[] bootStrap;

        //0x1be	 64	 MBR Partition Table, with 4 entries (below)
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public MBR_ptEntry[] first;
        //  0x1be	 16	 First partition table entry
        //  0x1ce	 16	 Second partition table entry
        //  0x1de	 16	 Third partition table entry
        //  0x1ee	 16	 Fourth partition table entry
        //0x1fe	 2	 (0x55, 0xAA) "Valid bootsector" signature bytes
        public ushort magic;
    }
}
